package com.xyrl.project.app.controller.sales;

import com.xyrl.common.base.exception.XyrlException;
import com.xyrl.common.util.IpAddrUtil;
import com.xyrl.project.common.authentication.LoginInfo;
import com.xyrl.project.common.base.service.CacheService;
import com.xyrl.project.common.base.vo.ResponseData;
import com.xyrl.project.common.common.service.AppService;
import com.xyrl.project.common.salesman.service.SalesmanService;
import com.xyrl.project.common.salesman.vo.UserLoginVo;
import com.xyrl.project.common.system.entity.LoginLog;
import com.xyrl.project.common.system.service.LoginLogService;
import com.xyrl.project.common.system.vo.UserVo;
import com.xyrl.project.common.util.CheckDeviceUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 *  * @className LoginController
 *  * @company 星纭融链科技服务有限公司
 *  * @author 徐恩源
 *  * @version 1.0
 *  * @date 2019-12-27 16:19:12
 *  * @Description 
 *  
 */
@RestController
@RequestMapping("/sales/login")
@Api(value = "登录", tags = "客户经理登录")
@Slf4j
public class SalesLoginController {
    @Autowired
    private SalesmanService salesmanService;
    @Autowired
    private LoginLogService loginLogService;
    @Autowired
    private CacheService cacheService;
    @Autowired
    private AppService appService;

    @ApiOperation("登录")
    @PostMapping
    public ResponseData login(HttpServletRequest request, @Valid UserLoginVo userLoginVo, @RequestHeader("user-agent") String userAgent) {
        LoginLog loginLog = new LoginLog();
        String ip = IpAddrUtil.getIpAddr(request);
        loginLog.setDeviceType(CheckDeviceUtil.getDevice(userAgent));
        loginLog.setDevice(userAgent);
        loginLog.setIp(ip);
        loginLog.setUsername(userLoginVo.getUsername());
        String errorMsg = "";
        try {
            UserVo user = salesmanService.loginBySales(userLoginVo);
            //登录用户id
            loginLog.setUserId(user.getUserId());
            LoginInfo info = new LoginInfo(user.getAccount(), user.getUserType());
            //缓存用户登录信息
            cacheService.cacheLoginUser(ip, info, user.getToken());
            user.setAppConfigVo(appService.getAppConfig());
            return ResponseData.success(user);
        } catch (XyrlException e) {
            errorMsg = e.getMessage();
            loginLog.setLoginResponse(errorMsg);
            log.error("客户经理登录异常", e);
        } catch (Exception e) {
            errorMsg = e.getMessage();
            loginLog.setLoginResponse(errorMsg);
            log.error("客户经理登录异常", e);
        } finally {
            // 保存登录记录
            loginLogService.saveLoginLog(loginLog);
        }
        return ResponseData.error(errorMsg);
    }

    @ApiOperation("退出登录")
    @PostMapping("loginOut")
    public ResponseData loginOut() {
        // 取得登录token 值
        String token = (String) SecurityUtils.getSubject().getPrincipal();
        cacheService.clearCacheLoginUser(token);
        return ResponseData.success("退出成功");
    }
}
